package com.lianmai.chaincard.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lianmai.chaincard.R;
import com.lianmai.chaincard.adapter.CityAdapter;
import com.lianmai.chaincard.bean.AreaInfo;
import com.lianmai.chaincard.utils.KeyBoardUtil;
import com.lianmai.chaincard.widget.ChineseSpelling;
import com.lianmai.chaincard.widget.LetterComparator;
import com.lianmai.chaincard.widget.PinnedHeaderDecoration;
import com.lianmai.chaincard.widget.SearchEditText;
import com.lianmai.chaincard.widget.WaveSideBarView;
import com.youth.xframe.base.XActivity;
import com.youth.xframe.widget.XToast;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

/**
 * 创建日期：2018/8/15 0015
 * 作者:Kite
 * 选择国家和地区
 */
public class SelectAreaActivity extends BaseActivity {

    @BindView(R.id.recycler_area)
    RecyclerView recyclerView;

    @BindView(R.id.edt_search_area)
    SearchEditText searchEditText;

    @BindView(R.id.side_view)
    WaveSideBarView waveSideBarView;

    @BindView(R.id.tv_search_area)
    TextView tvSearchArea;

    @BindView(R.id.tv_search_empty)
    TextView emptyTextView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_select_area;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        setTitle(R.string.countries_regions);
        initLogic();
    }

    private CityAdapter adapter;

    private ArrayList<AreaInfo> allList;

    private ArrayList<AreaInfo> arrayList = new ArrayList<>();

    private void initLogic() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        PinnedHeaderDecoration decoration = new PinnedHeaderDecoration();
        decoration.registerTypePinnedHeader(1, new PinnedHeaderDecoration.PinnedHeaderCreator() {
            @Override
            public boolean create(RecyclerView parent, int adapterPosition) {
                return true;
            }
        });
        recyclerView.addItemDecoration(decoration);
        adapter = new CityAdapter(null);
        recyclerView.setAdapter(adapter);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Type listType = new TypeToken<ArrayList<AreaInfo>>() {
                }.getType();
                Gson gson = new Gson();
                allList = gson.fromJson(AreaInfo.DATA, listType);
                Collections.sort(allList, new LetterComparator());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setNewData(allList);
                    }
                });
            }
        }).start();
        waveSideBarView.setOnTouchLetterChangeListener(new WaveSideBarView.OnTouchLetterChangeListener() {
            @Override
            public void onLetterChange(String letter) {
                int pos = adapter.getLetterPosition(letter);
                if (pos != -1) {
                    recyclerView.scrollToPosition(pos);
                    LinearLayoutManager mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    mLayoutManager.scrollToPositionWithOffset(pos, 0);
                }
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AreaInfo areaInfo = (AreaInfo) adapter.getItem(position);
                if (areaInfo != null) {
                    Intent intent = new Intent();
                    intent.putExtra("p0", areaInfo.areacode);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        searchEditText.setOnSearchClickListener(new SearchEditText.OnSearchClickListener() {
            @Override
            public void onSearchClick(View view) {
                searchArea();
            }
        });
        searchEditText.setOnClearClickListener(new SearchEditText.OnClearClickListener() {
            @Override
            public void onClearClick() {
                emptyTextView.setVisibility(View.GONE);
                waveSideBarView.setVisibility(View.VISIBLE);
                if (adapter != null) {
                    adapter.setNewData(allList);
                }
            }
        });
        tvSearchArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(searchEditText.getText().toString())) {
                    XToast.info("请输入要搜索的内容!");
                    return;
                }
                KeyBoardUtil.closeKeyboard(searchEditText, SelectAreaActivity.this);
                searchArea();
            }
        });
    }

    private void searchArea() {
        waveSideBarView.setVisibility(View.INVISIBLE);
        arrayList = ChineseSpelling.search(searchEditText.getText().toString(), allList, arrayList);
        emptyTextView.setVisibility(arrayList == null || arrayList.size() == 0 ? View.VISIBLE : View.GONE);
        if (adapter != null) {
            adapter.setNewData(arrayList);
        }
    }

}
